const app = getApp()

let we = require('../../we/index.js')
new class extends we.Page {
    data() {
        return {
            vo: {
                name: '',
                phone: '',
                address: '',
                addressHandle: '',
                prizeList: []
            },
            po: {
                id: ''
            }
        }
    }

    onLoad() {
        this.getAddressInfo()
        this.getPrizeList()
    }

    getPrizeList() {
        this.$post('/collage/prizeList').then(res=> {
            let arr = []
            res.map((item, index)=> {
                arr[index] = {}
                arr[index].name = item.product.name
                arr[index].joinId = item._id
                arr[index].num = index
                if (item.winner && item.status === 1) {
                    arr[index].status = '去支付'
                } else {
                    arr[index].status = ''
                }
            })
            this.setData({
                'vo.prizeList': arr
            })
        })
    }

    toPrizePage(event) {
        let prize = event.currentTarget.dataset.prize
        if (prize.status === '去支付') {
            wx.navigateTo({
                url: '/pages/lottery/index?payId=' + prize.joinId
            })
        }
    }

    getAddressInfo() {
        this.$post('/address/list').then(res => {
            if (res.length === 0) {
                this.setData({
                    'vo.addressHandle': '添加地址'
                })
            } else if (res.length > 0) {
                res.map((item, index)=> {
                    if (item.default) {
                        this.setData({
                            'vo.name': item.name,
                            'vo.phone': item.mobile,
                            'vo.address': item.province + item.city + item.area + item.address,
                            'po.id': item._id,
                            'vo.addressHandle': '点击修改'
                        })
                    }
                })
            }
        }).catch(err=> {
            wx.showModal({
                title: "错误",
                content: err.message,
                showCancel: false
            })
        })
    }

    chooseAddress() {
        wx.chooseAddress({
            success: (res) => {
                let pname = res.provinceName
                if (pname === '新疆' || pname === '西藏' || pname === '内蒙古' || pname === '黑龙江' || pname === '青海') {
                    wx.showModal({
                        title: "提示",
                        content: '暂不支持' + pname + '省的发货，请选择其他地址',
                        showCancel: false
                    })
                    return
                }
                let params = {
                    mobile: res.telNumber,
                    name: res.userName,
                    address: res.detailInfo,
                    province: res.provinceName,
                    city: res.cityName,
                    area: res.countyName
                }
                if (this.data.vo.addressHandle === '点击修改') {
                    Object.assign(params, ...{
                        id: this.data.po.id
                    })
                }
                this.postAddressInfo(params)
            },
            fail: (err)=> {
                if (err.errMsg === 'chooseAddress:cancel') {
                    return
                } else {
                    wx.showToast({
                        title: "授权失败，请小小程序的设置中打开'通讯录地址'",
                        icon: 'none'
                    })
                }
            }
        })
    }

    updateAddress() {
        let that = this
        wx.getSetting({
            success: (res)=> {
                if (!res.authSetting['scope.address']) {
                    wx.authorize({
                        scope: 'scope.address',
                        success: (res)=> {
                            that.chooseAddress()
                        },
                        fail: (res)=> {
                            wx.openSetting({
                                success: (res)=> {
                                    if (res.authSetting['scope.address']) {
                                        wx.showToast({
                                            title: "授权成功",
                                            icon: 'none'
                                        })
                                    } else {
                                        wx.showToast({
                                            title: "授权失败，请打开'通讯录地址'",
                                            icon: 'none'
                                        })
                                    }

                                },
                                fail: (res)=> {
                                    wx.showToast({
                                        title: "授权失败，请重试",
                                        icon: 'none'
                                    })
                                }
                            })
                        }
                    })
                }
                that.chooseAddress()
            }
        })
    }

    postAddressInfo(params) {
        this.$post('/address/edit', params).then(res=> {
            this.getAddressInfo()
            this.setData({
                'vo.addressHandle': '点击修改'
            })
        }).catch(err=> {
            wx.showModal({
                title: "错误",
                content: err.message,
                showCancel: false
            })
        })
    }
}